package CaseTraining;

import java.util.Random;
import java.util.Scanner;

public class CaseDemo10 {
    public static void main(String[] args) {
        int[] userNumber = userSelectNumber();
        System.out.println("您投注的号码是：");
        printRArray(userNumber);

        int [] luckNumber = createLuckNumber();
        System.out.println("中奖号码是：");
        printRArray(luckNumber);

        judge(userNumber, luckNumber);
    }
    public static void printRArray(int[] arr){
        //打印方法
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            int arrs = arr[i];
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
        }
        System.out.print("]");
        System.out.println();
    }

    public static int[] userSelectNumber(){
        //用户选择号码
        Scanner sc = new Scanner(System.in);
        //定义一组数组前面6个是红球号码（范围在1-33中间），后面1个是篮球号码（范围在1-16中间）
        int[] numbers = new int[7];
        //number = {0, 0, 0, 0, 0, 0, 0]
        //索引       0  1  2  3  4  5  6
        //遍历前六个数字，进行用户选数

        for (int i = 0; i < numbers.length - 1; i++) {
            while (true) {
                System.out.println("请您输入第" + (i + 1) + "个红球号码（1-33之间，不能重复）");
                int number = sc.nextInt();
                if(number < 1 || number > 33){
                    System.out.println("对不起，您输入的号码不在1-33之间，请重新输入！");
                }else {
                    if(exist(numbers,number)){
                        System.out.println("对不起，您当前输入的红球号码有重复，请重新输入！");
                    }else {
                        numbers[i] = number;
                        break;
                    }
                }
            }
        }

        // 投注蓝色球号码
        while (true) {
            System.out.println("请您输入最后一个蓝色球号码(1-16)：");
            int number = sc.nextInt();
            if(number < 1 || number > 16){
                System.out.println("对不起，您输入的号码不在1-16的范围内，请重新输入！");
            }else {
                numbers[6] = number;
                break;
            }
        }
        return numbers;
    }

    public static boolean exist(int[] numbers, int number) {
        //判断是否重复
        //number = {11, 12, 13, 14, 0, 0}
        for (int i = 0; i < numbers.length; i++) {
            if(numbers[i] == 0){
                break;
            }
            if(number == numbers[i]){
                return true;
            }
        }
        return false;
    }

    public static int[] createLuckNumber(){
        //定义一个数组里面有七个元素
        int[] numbers = new int[7];
        Random r = new Random();
        //对前6个循环，赋值
        for (int i = 0; i < numbers.length - 1; i++) {
            int number = r.nextInt(33) + 1;
            while (true) {
                if(!exist(numbers, number)){
                    numbers[i] = number;
                    break;
                }
            }
        }
        //给蓝色球随机值
        int number = r.nextInt(16) + 1;
        numbers[6] = number;
        return numbers;
    }

    public static void judge(int[] userNumber, int[] luckNumber){
        //userNumber:[12, 22, 31, 23, 16, 9, 7]
        //luckNumber:[9, 1, 7, 18, 27, 19, 1]
        int RedNumber = 0;
        int BlueNumber = 0;
        //对前6个数循环比对
        for (int i = 0; i < userNumber.length -1; i++) {
            for (int j = 0; j < luckNumber.length - 1; j++) {
                if(userNumber[i] == luckNumber[i]){
                    RedNumber++;
                    break;
                }
            }
        }
        BlueNumber = userNumber[6] == luckNumber[6] ? 1 : 0;
        System.out.println("您命中红球的数量是：" + RedNumber);
        System.out.println("您命中的蓝球数量是：" + BlueNumber);

        if(RedNumber == 6 && BlueNumber == 1){
            System.out.println("恭喜您中了1000万元");
        } else if (RedNumber == 6 && BlueNumber == 0) {
            System.out.println("恭喜您中了500万元");
        } else if (RedNumber == 5 && BlueNumber == 1) {
            System.out.println("恭喜您中了3000元");
        }else if (RedNumber == 5 && BlueNumber == 0  || RedNumber == 4 && BlueNumber == 1) {
            System.out.println("恭喜您中了200元");
        }else if (RedNumber == 5 && BlueNumber == 0  || RedNumber == 4 && BlueNumber == 1) {
            System.out.println("恭喜您中了200元");
        }else if (RedNumber == 4 && BlueNumber == 0  || RedNumber == 3 && BlueNumber == 1) {
            System.out.println("恭喜您中了10元");
        } else if (RedNumber <= 2 && BlueNumber == 1) {
            System.out.println("恭喜您中了5元");
        }else {
            System.out.println("感谢您对公益事业的奉献");
        }
    }
}
